﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace MMS
{
    public partial class Page_UpdataStaff : UIPage
    {
        public Page_UpdataStaff()
        {
            InitializeComponent();
        }
        db_mmsEntities conn = new db_mmsEntities();
        
        public string storeName { get; set; }
        
        int clickIndex;
        private void Page_UpdataStaff_Load(object sender, EventArgs e)
        {
            uiDataGridView5.SetRowHeight(30);
        }
        /// <summary>
        /// 查询员工信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Select_Click(object sender, EventArgs e)
        {
            uiDataGridView5.Rows.Clear();
            var temp = conn.tb_staff.Where(r => r.storeName == storeName).ToList();
            if (temp.Count == 0)
            {
                MessageBox.Show("未查询到员工信息");
                return;
            }
            foreach(var i in temp)
            {
                string[] staff = new string[] { i.staffID, i.staffName, i.phone, i.IDNumber, i.staffAddress, i.staffAge.ToString(), i.staffSex, i.isUse == 1 ? "激活" : "未激活" };
                uiDataGridView5.Rows.Add(staff);
            }
        }
        /// <summary>
        /// 点击视图表格内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiDataGridView5_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            clickIndex = e.RowIndex;
            //MessageBox.Show(index.ToString());
            //所在行商品编号
            txt_StaffNb.Text        = uiDataGridView5.Rows[clickIndex].Cells[0].Value.ToString();
            txt_StaffNm.Text        = uiDataGridView5.Rows[clickIndex].Cells[1].Value.ToString();
            txt_Phone.Text          = uiDataGridView5.Rows[clickIndex].Cells[2].Value.ToString();
            txt_IDNb.Text           = uiDataGridView5.Rows[clickIndex].Cells[3].Value.ToString();
            txt_StaffAddress.Text   = uiDataGridView5.Rows[clickIndex].Cells[4].Value.ToString();
            txt_StaffAge.Text       = uiDataGridView5.Rows[clickIndex].Cells[5].Value.ToString();
            com_StaffSex.Text       = uiDataGridView5.Rows[clickIndex].Cells[6].Value.ToString();
            com_StaffState.Text     = uiDataGridView5.Rows[clickIndex].Cells[7].Value.ToString();
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Reset_Click(object sender, EventArgs e)
        {
            string id = txt_StaffNb.Text;
            string name = txt_StaffNm.Text;
            string phone = txt_Phone.Text;
            string ID = txt_IDNb.Text;
            string address = txt_StaffAddress.Text;
            string age = txt_StaffAge.Text;
            string sex = com_StaffSex.Text;
            int state;
            if (com_StaffState.Text == "激活")
            {
                state = 1;
            }
            else if(com_StaffState.Text == "未激活")
            {
                state = 0;
            }
            else
            {
                MessageBox.Show("状态有误！");
                return;
            }
            var s =conn.tb_staff.ToList().FirstOrDefault(r => r.staffID == id);
            s.staffID = id;
            s.staffName = name;
            s.phone = phone;
            s.IDNumber = ID;
            s.staffAddress = address;
            s.staffAge = Convert.ToInt32(age);
            s.staffSex = sex;
            s.isUse = state;
            conn.SaveChanges();

            uiDataGridView5.Rows[clickIndex].Cells[0].Value = txt_StaffNb.Text;
            uiDataGridView5.Rows[clickIndex].Cells[1].Value = txt_StaffNm.Text;
            uiDataGridView5.Rows[clickIndex].Cells[2].Value = txt_Phone.Text;
            uiDataGridView5.Rows[clickIndex].Cells[3].Value = txt_IDNb.Text;
            uiDataGridView5.Rows[clickIndex].Cells[4].Value = txt_StaffAddress.Text;
            uiDataGridView5.Rows[clickIndex].Cells[5].Value = txt_StaffAge.Text;
            uiDataGridView5.Rows[clickIndex].Cells[6].Value = com_StaffSex.Text;
            uiDataGridView5.Rows[clickIndex].Cells[7].Value = com_StaffState.Text;
            MessageBox.Show("修改成功！");
        }                    
    }
}
